﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReportFactory.cs" company="">
//   
// </copyright>
// <summary>
//   The report factory.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.Report
{
    using System;

    /// <summary>
    /// The report factory.
    /// </summary>
    public class ReportFactory
    {
        #region Static Fields

        private static readonly ReportFactory Instance = new ReportFactory();

        #endregion

        #region Properties

        private ReportListener ReportListener { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The dispose.
        /// </summary>
        public static void Dispose()
        {
            Instance.ReportListenerCleanup();
        }

        /// <summary>
        /// The get report listener.
        /// </summary>
        /// <returns>
        /// The <see cref="Report.ReportListener"/>.
        /// </returns>
        /// <exception cref="NullReferenceException">
        /// </exception>
        public static ReportListener GetReportListener()
        {
            if (Instance.ReportListener == null)
            {
                throw new NullReferenceException("You MUST use ReportFactory.New() before calling this method");
            }

            return Instance.ReportListener;
        }

        /// <summary>
        /// The new.
        /// </summary>
        public static void New()
        {
            Instance.ReportListenerCleanup();
            Instance.ReportListenerInit();
        }

        #endregion

        #region Methods

        private void ReportListenerCleanup()
        {
            if (this.ReportListener != null)
            {
                this.ReportListener = null;
            }
        }

        private void ReportListenerInit()
        {
            if (this.ReportListener != null)
            {
                this.ReportListener = null;
            }

            this.ReportListener = new ReportListener();
        }

        #endregion
    }
}